<?php include("../config.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $sitename; ?> - Sysop: Manage Database</title>
<meta name="description" content="<?php echo $sitename; ?> - Sysop: Manage Database">
<meta name="keywords" content="free to try, software, shareware, freeware, developers, vendors, download, PAD">
<meta name="author" content="Andrey Aleksanyants, M.D.">
<meta name="copyright" content="easy HTools">
<meta http-equiv="imagetoolbar" content="no">
<link rel="stylesheet" type="text/css" href="../<?php echo $cssfile; ?>">
<link rel="shortcut icon" href="../favicon.ico">
</head>
<body>
<table class="tmain" align="center" width="90%" cellspacing="0" cellpadding="0">
<tr>
<td class="tmain">
<table class="tlogo" align="center" width="100%" cellspacing="0" cellpadding="4">
<tr>
<td align="right">
<p><a href="index.php">Home</a></p>
</td>
</tr>
</table>
</td>
</tr><tr>
<td class="tmain">
<br>
<table align="center" width="100%" border="0" cellspacing="0" cellpadding="1">
<tr valign="top">
<td class="tbody">
<h1><?php echo $sitename; ?> - Sysop: Manage Database</h1>
<?php

// Requesting form data
$qbutton  = $_POST["qbutton"];
$dbutton  = $_POST["dbutton"];
$sbutton  = $_POST["sbutton"];
$fields   = $_POST["fields"];
$table    = $_POST["table"];
$sort1    = $_POST["sort1"];
$sort2    = $_POST["sort2"];
$search   = $_POST["search"];
$company  = $_POST["company"];
$title    = $_POST["title"];
$sql      = $_POST["sql"];
$query    = $_POST["query"];

if((empty($qbutton) or empty($fields)) and empty($dbutton) and empty($sbutton)) {
	if(!($link_id = mysql_connect($dbhost, $userid, $userpassword))) {
		die("<p><b>Error connecting to the database server:</b> " . mysql_error() . "</p>\n");
	}
	$result = mysql_list_fields($dbname,"paddata");
	$n = mysql_num_fields($result);
	for($i=0; $i<$n; $i++) {
		$pf[$i] = mysql_field_name($result,$i);
	}
	mysql_close($link_id);

	echo "<h1>Author Data</h1>\n";
	echo "<p><b>Display</b></p>\n";
	echo "<form action=\"" . $PHP_SELF . "\" method=\"post\">\n";
	echo "<p>Query:\n";
	echo "<select name=\"query\">\n";
	for($i=0; $i<count($pf); $i++) {
		echo "<option>" . $pf[$i] . "</option>\n";
	}
	echo "</select> &nbsp;\n";
	echo "Value: <input type=\"text\" class=\"text\" name=\"search\" size=\"50\"></p>\n";
	echo "<p>Display:\n";
	echo "<select name=\"fields[]\" multiple size=\"4\">\n";
	for($i=0; $i<count($pf); $i++) {
		echo "<option>" . $pf[$i] . "</option>\n";
	}
	echo "</select>\n";
	echo "&nbsp; 1st Sort:\n";
	echo "<select name=\"sort1\">\n";
	for($i=0; $i<count($pf); $i++) {
		echo "<option>" . $pf[$i] . "</option>\n";
	}
	echo "</select>\n";
	echo "<input type=\"checkbox\" name=\"dsort1\">Desc\n";
	echo "&nbsp; 2nd Sort:\n";
	echo "<select name=\"sort2\">\n";
	for($i=0; $i<count($pf); $i++) {
		echo "<option>" . $pf[$i] . "</option>\n";
	}
	echo "</select>\n";
	echo "<input type=\"checkbox\" name=\"dsort2\">Desc</p>\n";
	echo "<input type=\"submit\" class=\"button\" name=\"qbutton\" value=\" Query paddata \">\n";
	echo "<input type=\"hidden\" name=\"table\" value=\"paddata\">\n";
	echo "</form><br>\n";
	echo "<hr>\n";

	echo "<p><b>Delete</b></p>\n";
	echo "<form action=\"" . $PHP_SELF . "\" method=\"post\">\n";
	echo "<p>Title: <input type=\"text\" class=\"text\" name=\"title\" size=\"44\"> &nbsp;\n";
	echo "Company: <input type=\"text\" class=\"text\" name=\"company\" size=\"24\"></p>\n";
	echo "<input type=\"submit\" class=\"button\" name=\"dbutton\" value=\"Delete program\">\n";
	echo "<input type=\"hidden\" name=\"table\" value=\"paddata\">\n";
	echo "</form><br>\n";
	echo "<hr>\n";

	echo "<h1>Raw SQL Query</h1>\n";
	echo "<form action=\"" . $PHP_SELF . "\" method=\"post\">\n";
	echo "<p>SQL command: <input type=\"text\" class=\"text\" size=\"76\" name=\"sql\"></p>\n";

	echo "<input type=\"submit\" class=\"button\" name=\"sbutton\" value=\"Run raw query\">\n";
	echo "</form><br>\n";

}
else {
	if(!($link_id = mysql_connect($dbhost, $userid, $userpassword))) {
		die("<p><b>Error connecting to the database server:</b> " . mysql_error() . "</p>\n");
	}
	mysql_select_db($dbname);

	if($qbutton && $fields) {
		$n = count($fields);
		if($dsort1) $sort1 .= " DESC";
		if($dsort2) $sort2 .= " DESC";
		$q = "SELECT * FROM $table ";
		if($search <> "") $q .= "WHERE $query LIKE '%$search%' ";
		$q .= "ORDER BY $sort1, $sort2";
		$result = mysql_query("$q", $link_id);

		echo "<p>Table: <b>" . $table . "</b></p>\n";
		echo "<p>SQL: $q</p><br>\n";

		echo "<table class=\"tvisible\" align=\"center\" cellspacing=\"0\" cellpadding=\"4\"><tr>\n";
		echo "<th class='tablehead'>No.</th>\n";
		for($i=0; $i<$n; $i++) {
			echo "<th class='tablehead'>",$fields[$i],"</th>\n";
		}
		echo "</tr>";

		$count = 0;
		while($query = mysql_fetch_array($result)) {
			$count++;
			echo "<tr><td>" . $count . "</td>";
			for($i=0; $i<$n; $i++) {
				$f = htmlspecialchars($query["$fields[$i]"]) . "&nbsp;";
				echo "<td>" . $f . "</td>";
			}
			echo "</tr>\n";
		}
		echo "</table>\n";
		echo "<h2 class=\"green\">Records found: " . $count . "</h2>\n";
	}

	if($dbutton) {
		$q = "DELETE FROM $table WHERE company = '$company'";
		if($table == "paddata") $q .= " AND title = '$title'";
		$result = mysql_query("$q", $link_id);
		$r = mysql_affected_rows($link_id);
		echo "<p>Table: <b>" . $table . "</b></p>\n";
		echo "<p>SQL: $q</p>\n";
		echo "<h2 class=\"green\">Records deleted: " . $r . "</h2>\n";
	}

	if($sbutton) {
		$sql = stripslashes($sql);
		echo "<p>SQL: ",$sql,"</p>\n";
		$result = mysql_query("$sql", $link_id);
		$r = @mysql_affected_rows($link_id);
		echo "<p>Affected rows = " . $r . "</p>\n";

		$n = 0;
		$n = @mysql_num_fields($result);
		if($n > 0) {
			echo "<table class=\"tvisible\" align=\"center\" cellspacing=\"0\" cellpadding=\"4\"><tr>\n";
			echo "<th class=\"tablehead\">No.</th>\n";
			for($i=0; $i<$n; $i++) {
				echo "<th class=\"tablehead\">" . mysql_field_name($result,$i) . "</th>\n";
			}
			echo "</tr>\n";

			$count = 0;
			while($query = mysql_fetch_row($result)) {
				$count++;
				echo "<tr><td>" . $count . "</td>\n";
				for($i=0; $i<$n; $i++) {
					$f = htmlspecialchars($query[$i]) . "&nbsp;";
					echo "<td>" . $f . "</td>\n";
				}
				echo "</tr>\n";
			}
			echo "</table>\n";
			echo "<h2 class=\"green\">Records found: " . $count . "</h2>\n";
		}
	}
	mysql_close($link_id);
}
?>
<br>

</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>